package Fisica;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Logica.Util;
import Modelo.Tarjeta;
import Modelo.TarjetaRelacion;

public class DAORelacion {

	public static String SELECT = "select * from tarjeta_relacion";	
	public static String FIRST = "SELECT * FROM tarjeta_relacion order by id asc;";
	public static String LAST = "SELECT * FROM tarjeta_relacion order by id desc;";
	public static String PRE = "SELECT * FROM tarjeta_relacion WHERE id < ? order by id desc";
	public static String POST = "SELECT * FROM tarjeta_relacion WHERE id > ? order by id asc";
	public static String INSERT = "INSERT INTO tarjeta_relacion (relacion) VALUES (?);";
	public static String UPDATE = "UPDATE tarjeta_relacion SET relacion = ? WHERE id = ?;";
	
	
	public static Modelo.TarjetaRelacion get (int id){
		String sql = SELECT + " where id = ?;";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			st.setInt(1, id);
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				Modelo.TarjetaRelacion a = new Modelo.TarjetaRelacion(); 
				a.setId(rs.getInt("id")); 	
				a.setRelacion(rs.getString("relacion"));
				
				
				return a;
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return new TarjetaRelacion();
	}
	
	public static Modelo.TarjetaRelacion get (String rel){
		String sql = SELECT + " where relacion = ?;";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			st.setString(1, rel);
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				Modelo.TarjetaRelacion a = new Modelo.TarjetaRelacion(); 
				a.setId(rs.getInt("id")); 	
				a.setRelacion(rs.getString("relacion"));				
				
				return a;
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return new TarjetaRelacion(-1, "");
	}
	
	
	public static List<Object> getList (){
		List<Object> list = new ArrayList();
		String sql = SELECT +";";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			TarjetaRelacion a;
			while (rs.next()){				
				a = new Modelo.TarjetaRelacion(); 
				a.setId(rs.getInt("id")); 	
				a.setRelacion(rs.getString("relacion"));
				
				list.add(a);
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return list;
	}
	
	 public boolean insert(Object obj) { //ok
			TarjetaRelacion v = (TarjetaRelacion)obj;
			
			try {			
				PreparedStatement st;
				ResultSet rs;
				//INSERTO
				st = Conexion.getConexion().prepareStatement(INSERT);	
				st.setString(1, v.getRelacion());
				st.executeUpdate();
				//RECUPERO
				st = Conexion.getConexion().prepareStatement(SELECT + " where relacion = ?;");
				st.setString(1, v.getRelacion());
				rs = st.executeQuery();
				if(rs.next())
					v.setId(rs.getInt("id"));
				System.out.println("inserto: "+v.getId());

				return true;
				
			}catch(Exception e){
				System.out.println("exception al intentar insertar: "+v.getId());
				e.printStackTrace();
			}
			return false;				
		}
	 
	 public boolean update(Object obj) { //ok
			TarjetaRelacion v = (TarjetaRelacion)obj;
			
			try {			
				PreparedStatement st;
				ResultSet rs;
				//ACTUALIZO
				st = Conexion.getConexion().prepareStatement(UPDATE);	
				st.setString(1, v.getRelacion());
				st.setInt(2, v.getId());
				st.executeUpdate();
				//RECUPERO
				st = Conexion.getConexion().prepareStatement(SELECT + " where relacion = ?;");
				st.setString(1, v.getRelacion());
				rs = st.executeQuery();
				if(rs.next())
					v.setId(rs.getInt("id"));
				System.out.println("actualizo: "+v.getId());

				return true;
				
			}catch(Exception e){
				System.out.println("exception al actualizo insertar: "+v.getId());
				e.printStackTrace();
			}
			return false;				
		}
	 
	 public Object getObject (PreparedStatement st){ //ok
			TarjetaRelacion a = new TarjetaRelacion(); 	
			try {
				ResultSet rs = st.executeQuery();			
				FileOutputStream archivofoto;
				File file; 
				if (rs.next()){
					a.setId(rs.getInt("id")); 	
					a.setRelacion(rs.getString("relacion"));					
				}	
				return a;
			} catch (SQLException e) {
				System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
				e.printStackTrace();
			} 		
			return new Tarjeta();		
		}

		public Object getPre(int id) {
			try {
				PreparedStatement st = Conexion.getConexion().prepareStatement(PRE);
				st.setInt(1, id);
				return getObject(st);
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
			return null;
		}

		public Object getPost(int id) {
			try {
				PreparedStatement st = Conexion.getConexion().prepareStatement(POST);
				st.setInt(1, id);
				return getObject(st);
			} catch (SQLException e) {			
				e.printStackTrace();
			}		
			return null;
		}
		
		public Object getFirst() {
			try {
				PreparedStatement st = Conexion.getConexion().prepareStatement(FIRST);
				return getObject(st);
			} catch (SQLException e) {			
				e.printStackTrace();
			}		
			return null;
		}
		
		public Object getLast() {
			try {
				PreparedStatement st = Conexion.getConexion().prepareStatement(LAST);
				return getObject(st);			
			} catch (SQLException e) {			
				e.printStackTrace();
			}		
			return null;
		}
	 
}
